<template>
	<view class="coupon-page" v-if="couponDetail">
		<view class="coupon-info-box">
			<view class="coupon-info">
				<view class="item-left active">
					<view class="coupon-top" v-if="couponDetail.type==2">此商家可用</view>
					<view class="coupon-top" v-if="couponDetail.type==1">全场通用</view>
					<view class="coupon-money">
						<text class=" coupon-unit">¥</text>{{couponDetail.number}}
					</view>
					<view class="coupon-rule" v-if="couponDetail.money!=0">满{{couponDetail.money}}可用</view>
					<view class="coupon-rule" v-else>无门槛</view>
				</view>
				<view class="item-right">
					<view class="item-title">{{couponDetail.name}}</view>
					<view class="item-type">到期时间:{{couponDetail.endtime}}</view>
					<view class="item-time">
						<view>数量:{{couponDetail.give_num}}</view>
						<view>积分兑换:{{couponDetail.score?couponDetail.score+'积分':'免费领取'}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="tips-box">
			<view class="title">使用说明</view>
			<view class="content">{{couponDetail.content}}</view>
		</view>
		<view class="footer-box" v-if="pageType=='receive'">
			<view class="button" @click="$u.throttle(couponReceive,2000)">领取</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageType: 'detail', //页面类型 领取 详情
				coupon_id: null,
				couponDetail: null
			}
		},
		onLoad(options) {
			if (options && options.page) {
				this.pageType = options.page
			}
			if (options && options.coupon_id) {
				this.coupon_id = options.coupon_id
				this.getCouponDetail()
			}
		},
		methods: {
			couponReceive() {
				let that = this
				uni.showModal({
					title: "提示",
					content: "兑换此优惠券需要消耗" + this.couponDetail.score + "积分，是否兑换?",
					success(res) {
						if (res.confirm) {
							that.$Api.userMerchant.couponPay({
								id: that.coupon_id,
							}).then((res) => {
								that.getCouponDetail()
								that.$Tips({
									title: '领取成功'
								}, {
									tab: 4,
									url: '/pages/common/receiveSuccess'
								})
							});
						}
					}
				})
			},
			getCouponDetail() {
				this.$Api.userMerchant.getCouponDetail({
					id: this.coupon_id,
				}).then((res) => {
					this.couponDetail = res.data;
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.coupon-page {
		height: 100%;
		min-height: 100vh;
		background: #ffffff;

		.tips-box {
			padding: 20rpx 30rpx;

			.title {
				font-weight: bold;
				font-size: 32rpx;
			}

			.contnet {
				margin-top: 20rpx;
				font-size: 26rpx;
				color: #999CA7;
			}
		}

		.coupon-info-box {
			padding: 20rpx 30rpx;
			background: #F6F6F8;

			.coupon-info {
				display: flex;
				align-items: center;
				box-sizing: border-box;
				box-shadow: 0rpx 0rpx 12rpx 0rpx rgba(0, 0, 0, 0.1), inset 0rpx 2rpx 2rpx 0rpx #FFFFFF;
				border-radius: 28rpx;
				background: #ffffff;

				.item-left {
					width: 180rpx;
					height: 228rpx;
					margin-right: 20rpx;
					background-color: #d6d6d6;
					color: #FFFFFF;

					.coupon-top {
						font-weight: 400;
						font-size: 14px;
						line-height: 21px;
						text-align: center;
						padding: 10rpx 0;
						background: linear-gradient(90deg, #FFE2E2 0%, #FFF3F3 100%);
						border-radius: 14px 0px 0px 0px;
					}

					.coupon-money {
						font-weight: bold;
						font-size: 26px;
						line-height: 39px;
						text-align: center;
						margin-top: 12rpx;

						.coupon-unit {
							font-weight: bold;
							font-size: 26rpx;
							text-align: center;
						}
					}

					.coupon-rule {
						margin-top: 12rpx;
						font-weight: 400;
						font-size: 14px;
						line-height: 21px;
						text-align: center;
					}
				}

				.active {
					background-color: #fff3f3;
					color: #FF1E00;
				}

				.item-right {
					flex: 1;
					height: 228rpx;
					display: flex;
					flex-direction: column;
					justify-content: center;
					margin-right: 20rpx;

					.item-title {
						font-weight: bold;
						font-size: 32rpx;
					}

					.item-type {
						font-size: 26rpx;
						color: #565656;
						margin-top: 16rpx;
					}

					.item-time {
						display: flex;
						justify-content: center;
						justify-content: space-between;
						font-size: 26rpx;
						color: #565656;
						margin-top: 16rpx;
					}
				}
			}

		}

		.footer-box {
			width: 100%;
			height: 216rpx;
			position: fixed;
			bottom: 0;
			background: #fff;
			box-shadow: 0rpx -4rpx 8rpx 0rpx rgba(0, 0, 0, 0.1);
			padding: 20rpx 38rpx;
			box-sizing: border-box;

			.button {
				width: 100%;
				height: 88rpx;
				line-height: 88rpx;
				text-align: center;
				color: #ffffff;
				background: #a3c616;
				box-shadow: 0rpx 4rpx 12rpx 0rpx rgba(163, 198, 22, 0.3);
				border-radius: 24rpx;
				font-weight: 500;
				font-size: 34rpx;
				margin-top: 18rpx;
			}
		}
	}
</style>